﻿using Dapper;
using HR.Config;
using HR.IService;
using HR.Models;
using System.Data.SqlClient;

namespace HR.Service
{
    public class HumanFileDeleService : IHumanFileDeleService
    {
        private readonly HRDBContext db;
        private readonly IConfiguration icon;
        private readonly string? sqlcon;

        public HumanFileDeleService(HRDBContext db, IConfiguration icon)
        {
            this.db = db;
            this.icon = icon;
            sqlcon = icon["ConnectionStrings"];
        }

        public Task<Paging<human_file>> HumanFileFind(HFSelect hs)
        {
            return Task.Run(() =>
            {
                Paging<human_file> pg = new Paging<human_file>();
                using (SqlConnection sqlc = new SqlConnection(sqlcon))
                {
                    string sql = $@"select * from [dbo].[human_file] where human_file_status = '{hs.human_file_status}'";
                    if (hs.FSTkinds[0] != null)
                    {
                        //将字符串拆分成字符串数组
                        hs.FSTkinds = hs.FSTkinds[0].Split(",");
                        sql += @$" and [first_kind_id]='{hs.FSTkinds[0]}' and [second_kind_id]='{hs.FSTkinds[1]}' and third_kind_id='{hs.FSTkinds[2]}' ";
                    }
                    if (hs.human_majors[0] != null)
                    {
                        //将字符串拆分成字符串数组后将长度不足两位的左侧补零
                        hs.human_majors = hs.human_majors[0].Split(",");
                        for (int i = 0; i < hs.human_majors.Length; i++)
                        {
                            hs.human_majors[i] = hs.human_majors[i].PadLeft(2, '0');
                        }
                        //这里还需要判断一下sql语句中是否包含了where字符串，如果有，则是用and拼接，没有则是拼接第一段where
                        sql += @$" and human_major_kind_id='{hs.human_majors[0]}' and [human_major_id]='{hs.human_majors[1]}'";
                    }
                    if (hs.regist_timeClose != "undefined" && hs.regist_timeClose != null)
                    {
                        sql += @$" and [regist_time] between '{hs.regist_timeOpen}' and '{hs.regist_timeClose}'";
                    }
                    List<human_file> list = sqlc.Query<human_file>(sql).ToList();
                    List<human_file> tabs = list.Skip((hs.pagecurrent - 1) * hs.pagesize).Take(hs.pagesize).ToList();
                    pg.rows = list.Count;
                    pg.tabs = tabs;
                }
                return pg;
            });

        }

        public Task<int> HumanFileUdpDel(short huf_id)
        {
            return Task.Run(() =>
            {
                human_file? hf = db.human_files.Find(huf_id);
                //将档案状态进行修改，这里改成已删除
                hf.human_file_status = false;
                return db.SaveChanges();
            });
        }


        public Task<int> HumanFileUdpRes(short huf_id)
        {
            return Task.Run(() =>
            {
                human_file? hf = db.human_files.Find(huf_id);
                //将档案状态进行修改，这里改成已删除
                hf.human_file_status = true;
                return db.SaveChanges();
            });
        }


        public Task<Paging<human_file>> HumanFileUdpDelFinds(int pagesize, int pagecurrent)
        {
            return Task.Run(() =>
            {
                List <human_file> list = db.human_files.Where(hf=>hf.human_file_status==false).ToList();
                List<human_file> tabs = list.Skip((pagecurrent - 1) * pagesize).Take(pagesize).ToList();
                //将档案状态进行修改，这里改成已删除
                Paging<human_file> pg = new Paging<human_file>()
                {
                    tabs = tabs,
                    rows = list.Count
                };
                return pg;
            });
        }

        public Task<int> HumanFileDelete(short huf_id)
        {
            return Task.Run(() =>
            {
                human_file? hf = db.human_files.Find(huf_id);
                db.human_files.Remove(hf);
                return db.SaveChanges();
            });
        }
    }
}
